// pages/shareArticle/index.js
const {
  post
} = getApp();
Page({
  data: {
    params: {
      name: "",
      page: 1,
      pageSize: 20
    },
    showLoading: true,
    dataList: [],
    showPage: false
  },
  // 初始化加载
  onLoad: function () {
    this.getList();
  },
  // 获取数据
  getList() {
    if (this.data.showLoading) {
      post("/spread/article/list", this.data.params).then(res => {
        if (res.result == 1) {
          const dataList = this.data.dataList.concat(res.data);
          this.setData({
            dataList,
            showPage: true
          });
          if (res.data.length < this.data.params.pageSize) {
            this.setData({
              showLoading: false
            })
          }
        };
      });
    }
  },
  // 上拉加载
  onReachBottom: function (e) {
    this.setData({
      "params.page": this.data.params.page += 1
    });
    this.getList();
  },
  // 输入框确定
  searchValue(e) {
    this.setData({
      "params.page": 1,
      "dataList": [],
      "params.name": e.detail,
      "showLoading": true,
      "showPage": false
    });
    this.getList();
  },
  // 跳转页面
  goTourl(e) {
    wx.navigateTo({
      url: `../articleDetail/index?id=${e.currentTarget.dataset.id}`,
    })
  },
  // 分享
  onShareAppMessage: function (e) {
    return {
      title: e.target.dataset.title,
      path: `/pages/articleDetail/index?id=${e.target.dataset.id}`,
      imageUrl: e.target.dataset.cover,
      success: function (res) {
        console.log("分享成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        console.log("分享失败:" + JSON.stringify(res));
      }
    }
  },
  onShow: function () {
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    });
  }
})